package com.love_weinuo.util.dialog;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;

import com.tencent.qcloud.uikit.common.utils.ImageUtil;
import com.tencent.qcloud.uikit.common.widget.gatherimage.BitmapUtils;
import com.love_weinuo.R;
import com.love_weinuo.bean.NearPindanCallBackBean;
import com.love_weinuo.databinding.DialogZxingBinding;
import com.love_weinuo.ui.activity.SettingActivity;
import com.love_weinuo.util.DpUtil;
import com.love_weinuo.util.ImageUtils;
import com.love_weinuo.util.ToastUtil;
import com.love_weinuo.util.listener.OnBHClickListener;
import com.love_weinuo.util.timeutil.TimeUtil;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.io.File;

/**
 * Created by Administrator on 2019/3/24.
 */

public class ZxingDialog extends BaseDialog {

    Context context;
    DialogZxingBinding binding;
    OnBHClickListener onBHClickListener;
    String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "zhenhanghangkong";

    public ZxingDialog(Context context) {
        super(context);
        this.context = context;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_zxing, null, false);
        binding.setDialog(this);
        setContentView(binding.getRoot());
    }

    Bitmap mBitmap;
    public void createShow(NearPindanCallBackBean.ResultEntity.ResEntity bean,String inviteCode) {
        int width = DpUtil.getInstance().getScreenWidth(context);
        mBitmap = CodeUtils.createImage(new StringBuffer("振航航空&"+bean.getId() + "&" + inviteCode ).toString(),
                width, width, BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_app_logo));
        binding.image.setImageBitmap(mBitmap);
    }

    public void setOnBHClickListener(OnBHClickListener onBHClickListener) {
        this.onBHClickListener = onBHClickListener;
    }

    public void noPoint(View view) {
        if (onBHClickListener != null) {
            onBHClickListener.onCloseClick();
        }
        if (ImageUtils.saveImageToGallery(context, mBitmap,storePath)) {
            /** * 分享图片 */
            Intent share_intent = new Intent();
            share_intent.setAction(Intent.ACTION_SEND);
            share_intent.setType("image/*");
            share_intent.putExtra(Intent.EXTRA_STREAM, ImageUtils.saveBitmap(mBitmap,"img"));
            share_intent.putExtra(Intent.EXTRA_TEXT, "我发起了一个拼单");
            share_intent = Intent.createChooser(share_intent, "振航航空");
            getActivity().startActivity(share_intent);
        }
        dismiss();
    }

    public void okPoint(View view) {
        if (onBHClickListener != null) {
            onBHClickListener.onConfirmClick();
        }
        if (ImageUtils.saveImageToGallery(context, mBitmap,storePath)) {
            ToastUtil.showToast("保存成功");
        }
        dismiss();
    }
}

